package com.tian.common.util

import java.lang.Exception
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author tiansongtao, Email:tiansongtao@up366.com
 * @date 2020/12/17 14:08
 */
object TimeUtil {

    /**
     * 把时间毫秒值转换成指定格式的日期字符串
     *
     * @param milliSeconds 时长
     * @param pattern      yyyy-MM-dd HH:mm:ss yyyy年-MM月-dd日 yyyy-MM-dd E HH:mm:ss
     *
     * @return 日期字符串
     */
    fun formatTime(milliSeconds: Long, pattern: String) = SimpleDateFormat(pattern).format(Date(milliSeconds))

    fun formatTime(date: Date, pattern: String) = formatTime(date.time, pattern)


    fun parseTime(time: String?, pattern: String): Date? {
        if (time.isNullOrEmpty()) { return null }
        val format = SimpleDateFormat(pattern)
        var date: Date? = null
        try {
            date = format.parse(time)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return date
    }
}